/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.market.web.controller;

import com.market.web.model.Customer;
import java.io.IOException;
import java.io.PrintWriter;
import javax.json.Json;
import javax.json.JsonObjectBuilder;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Mai
 */
@WebServlet({"/header","/chatFunc"})
public class headerController extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
    String userPath = req.getServletPath();
    System.out.println(userPath);
    HttpSession session = req.getSession();

    Customer c = (Customer)session.getAttribute("cust");
    if(userPath.equals("/header"))
        {
            
            if(c == null)
            {
                System.out.println("firstly null");
            }
            else
            {
                JsonObjectBuilder user = Json.createObjectBuilder();
                    user.add("username", c.getName());
                    
                    try (PrintWriter pw = resp.getWriter()) {
                    pw.println(user.build().toString());
                    
                }
          
            }
            
        }
    else if(userPath.equals("/chatFunc"))
    {
        if(c == null)
        {
            System.out.println("no login user");
        }
        else
        {
            JsonObjectBuilder user = Json.createObjectBuilder();
                user.add("username", c.getName());

                try (PrintWriter pw = resp.getWriter()) {
                pw.println(user.build().toString());

            }

        }
    }
}
 
    
    
    
    
}
